const { ccclass, property } = cc._decorator;

enum ElementType {
    MatchItem = 1,
    PropItem = 2,
    ObstacleItem = 3,
    Null = 4
}

@ccclass
export default class FixedElementItemCtrl extends cc.Component {
    @property(cc.Node)
    elementNode: cc.Node = null;

    pType: ElementType = null;
    cType: any = null;
    hp: number = null;
    isHaveAdd: boolean = false;

    start() {
        // TODO: Implement start method
    }

    changeSp(e) {
        const t = e.squareType;
        let a = null;

        if (t == ElementType.MatchItem) {
            a = e.matchType;
        } else if (t == ElementType.PropItem) {
            a = e.propType;
        } else if (t == ElementType.ObstacleItem) {
            a = ObstacleAllType;
            this.hp = e.obstacleHp;
        }

        if (a != null && t != null) {
            if (this.pType !== t || this.cType !== a) {
                this.pType = t;
                this.cType = a;
                this.isHaveAdd = false;

                const o = e.node.getComponent(cc.Sprite).spriteFrame;
                this.elementNode.getComponent(cc.Sprite).spriteFrame = o;
            }
        } else {
            console.log("固定刷新的父节点子节点获取不正确");
        }
    }

    resetSp() {
        this.elementNode.getComponent(cc.Sprite).spriteFrame = null;
        this.pType = null;
        this.cType = null;
        this.isHaveAdd = false;
    }

    getInitStr() {
        let e = "";

        if (this.pType === null && this.cType === null) {
            e = "N";
        } else {
            let t = "";

            if (this.pType === ElementType.MatchItem) {
                t = "M";
            } else if (this.pType === ElementType.PropItem) {
                t = "P";
            } else if (this.pType === ElementType.ObstacleItem) {
                t = "O";

                if (this.hp > 0) {
                    e = t + "-" + this.cType + "_" + this.hp;
                } else {
                    e = t + "-" + this.cType;
                }
            }

            e = t + "-" + this.cType;
        }

        return e;
    }
}
